package cn.stylefeng.guns.modular.wordstudy.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.wordstudy.entity.Trans;
import cn.stylefeng.guns.modular.wordstudy.mapper.TransMapper;
import cn.stylefeng.guns.modular.wordstudy.model.params.TransParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.TransResult;
import  cn.stylefeng.guns.modular.wordstudy.service.TransService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2020-11-22
 */
@Service
public class TransServiceImpl extends ServiceImpl<TransMapper, Trans> implements TransService {

    @Override
    public void add(TransParam param){
        Trans entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TransParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TransParam param){
        Trans oldEntity = getOldEntity(param);
        Trans newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TransResult findBySpec(TransParam param){
        return null;
    }

    @Override
    public List<TransResult> findListBySpec(TransParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TransParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TransParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Trans getOldEntity(TransParam param) {
        return this.getById(getKey(param));
    }

    private Trans getEntity(TransParam param) {
        Trans entity = new Trans();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
